package com.zs.toolbox.web2j.system.core.controller.common;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPrimaryKeyReqeust;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAdminBaseController;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.common.web.WtCommonPropertiesSetter;
import com.zs.toolbox.web2j.system.core.entity.WtPostCommentEntity;
import com.zs.toolbox.web2j.system.core.entity.WtPostCommentWithUserInfoEntity;
import com.zs.toolbox.web2j.system.core.request.WtCommentPageQueryModel;
import com.zs.toolbox.web2j.system.core.request.WtPostCommentRequest;
import com.zs.toolbox.web2j.system.core.service.WtPostCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zs.toolbox.web2j.common.exception.WtResponses.SUCCESS;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-11
 */
@RequestMapping("/api/v1/admin/post/comment")
@Api(description = "评论管理")
@Slf4j
@RestController
public class WtCommonPostCommentController extends WtAdminBaseController<WtPostCommentService, WtPostCommentEntity,Long> {

    @Autowired
    WtPostCommentService wtPostCommentService;


    @ApiOperation("添加新评论")
    @PreAuthorize("isAuthenticated()")
    public WtBaseResponse<String> savePost(WtPostCommentRequest wtPostContentRequest) {
        this.volidate(wtPostContentRequest);

        WtPostCommentEntity wtPostCommentEntity = new WtPostCommentEntity();
        WtCommonPropertiesSetter.setDefaultCommonProperty(wtPostCommentEntity);
        BeanUtils.copyProperties(wtPostContentRequest,wtPostCommentEntity);

        wtPostCommentEntity.setUserId(WtAuthContext.getUserId());
        wtPostCommentEntity.setCreateUser(WtAuthContext.getUserId());
        wtPostCommentService.saveComment(wtPostCommentEntity);
        return SUCCESS;
    }

    @RequestMapping("/detail")
    @ApiOperation("查看单条评论内容")
    public WtBaseResponse<WtPostCommentWithUserInfoEntity> postDetail(WtPrimaryKeyReqeust<Long> reqeust) {
        this.volidate(reqeust);
        WtPostCommentWithUserInfoEntity wtPostDetailWithUserInfoEntity = wtPostCommentService
                .fetchPostDetailById(reqeust.getId());
        return new WtBaseResponse<>(wtPostDetailWithUserInfoEntity);
    }

    @RequestMapping("/page")
    @ApiOperation("根据文章ID或评论ID查询评论")
    public WtBaseResponse<WtPageBodyModel<WtPostCommentWithUserInfoEntity>> pagePost(WtCommentPageQueryModel wtPageQueryModel) {
        this.volidate(wtPageQueryModel);

        WtPageBodyModel<WtPostCommentWithUserInfoEntity> page = wtPostCommentService.pageComment(wtPageQueryModel);
        return new WtBaseResponse(page);
    }


    @RequestMapping("/delete")
    @ApiOperation("删除评论")
    public WtBaseResponse delete(WtPrimaryKeyReqeust<Long> reqeust) {
        this.volidate(reqeust);
        wtPostCommentService.remove(new LambdaQueryWrapper<WtPostCommentEntity>()
                .eq(WtPostCommentEntity::getCommentId, reqeust.getId()).eq(WtPostCommentEntity::getUserId
                        , WtAuthContext.getUserId()));
        return SUCCESS;
    }


}
